<template>
  <div id="app">
    <div class="tab-container">
      <div class="tabs">
        <router-link to="/" class="tab" active-class="active">首页</router-link>
        <router-link to="/about" class="tab" active-class="active">关于</router-link>
        <router-link to="/user" class="tab" active-class="active">用户</router-link>
      </div>
      <div v-if="isLoggedIn" class="user-info">
        <span>{{ loginSource }}已登录</span>
        <button v-if="!isFromMainApp" @click="handleLogout" class="logout-btn">退出登录</button>
      </div>
    </div>
    
    <div class="content">
      <!-- 路由视图 -->
      <router-view></router-view>
    </div>
  </div>
</template>

<script>
import { ref, computed, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { qiankunWindow } from 'vite-plugin-qiankun/dist/helper';

export default {
  name: "App",
  setup() {
    const router = useRouter();
    const isLoggedIn = ref(false);
    const isFromMainApp = ref(false);
    const loginSource = computed(() => isFromMainApp.value ? '主应用' : '子应用');
    
    // 检查登录状态
    const checkLoginStatus = () => {
      // 检查是否从主应用获取了token
      const mainAppToken = localStorage.getItem('mainAppToken');
      const subAppToken = localStorage.getItem('subAppToken');
      
      if (mainAppToken) {
        isLoggedIn.value = true;
        isFromMainApp.value = true;
      } else if (subAppToken) {
        isLoggedIn.value = true;
        isFromMainApp.value = false;
      } else {
        isLoggedIn.value = false;
        isFromMainApp.value = false;
      }
    };
    
    // 退出登录
    const handleLogout = () => {
      localStorage.removeItem('subAppToken');
      localStorage.removeItem('subAppUsername');
      isLoggedIn.value = false;
      router.push('/login');
    };
    
    onMounted(() => {
      console.log("firstApp mounted");
      checkLoginStatus();
      
      // 判断是否是从主应用进入
      isFromMainApp.value = qiankunWindow.__POWERED_BY_QIANKUN__;
    });
    
    return {
      isLoggedIn,
      isFromMainApp,
      loginSource,
      handleLogout
    };
  },
  mounted() {
    console.log("firstApp mounted");
  },
};
</script>

<style>
#app {
  font-family: Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
  margin: 0;
  padding: 0;
}

.tab-container {
  background-color: #f5f5f5;
  padding: 10px;
  border-bottom: 1px solid #ddd;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.tabs {
  display: flex;
  gap: 20px;
}

.tab {
  padding: 8px 16px;
  text-decoration: none;
  color: #333;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.tab:hover {
  background-color: #e0e0e0;
}

.tab.active {
  background-color: #1976d2;
  color: white;
}

.content {
  padding: 20px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logout-btn {
  padding: 4px 8px;
  background-color: #f44336;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.logout-btn:hover {
  background-color: #d32f2f;
}
</style>
